package climspace.hadoop;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class UploadToHDFS {
	private Configuration config;
	// default value
	private String destinationFolder = "mesowest/";
	
	public UploadToHDFS(String host, String port) {
		config = new Configuration();
		config.set("fs.default.name", "hdfs://"+host+":"+port);
		try {
			FileSystem fs = FileSystem.get(config);
			Path destination = new Path(destinationFolder);
			if (fs.exists(destination)==false)
				fs.mkdirs(destination);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void copyLocalToRemote(String filename) {
		try {
			FileSystem fs = FileSystem.get(config);
			fs.copyFromLocalFile(new Path(filename), new Path(destinationFolder));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getDestinationFolder() {
		return destinationFolder;
	}

	public void setDestinationFolder(String destinationFolder) {
		this.destinationFolder = destinationFolder;
	}

	/**
	 * @param args "C:/data/dropbox/cmpe275/project1/sample data/mesowest.out"
	 */
	public static void main(String args[]) {
		if (args.length != 1)
			throw new RuntimeException("Incorrent number of parameter");
	
		String fileName = args[0];
		
		UploadToHDFS u = new UploadToHDFS("localhost", "9000");
		u.copyLocalToRemote(fileName);
		
	}
}
